<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
       <el-form-item label="分类名称" prop="name">
         <el-input
             v-model="queryParams.name"
             placeholder="请输入分类名称"
             clearable
             style="width: 200px"
             @keyup.enter="handleQuery"
         />
       </el-form-item>
       <el-form-item label="开启状态" prop="status">
        <el-select v-model="queryParams.status" clearable filterable placeholder="请选择开启状态">
          <el-option
            v-for="item in dict.type.comm_kaiguan_status"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          @click="handleAdd()"
          v-hasPermi="['trade:serverCategory:add']"
        >新增
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 表格数据 -->
    <el-table v-loading="loading" :data="list" default-expand-all row-key="id" :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
      <el-table-column label="分类名称" align="center" prop="name"/>
      <el-table-column label="分类图" align="center" prop="picUrl" >
        <template slot-scope="{row}">
          <el-image v-if="row.picUrl"
            style="width: 100px; height: 100px"
            :src="row.picUrl"
            :preview-src-list="[row.picUrl]">
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="分类场景图" align="center" prop="topPicUrl" >
        <template slot-scope="{row}">
          <el-image v-if="row.topPicUrl"
            style="width: 100px; height: 100px"
            :src="row.topPicUrl"
            :preview-src-list="[row.topPicUrl]">
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="分类排序" align="center" prop="sort"/>
      <el-table-column label="开启状态" align="center" prop="status">
        <template slot-scope="{row}">
          <dict-tag :options="dict.type.comm_kaiguan_status" :value="row.status" />
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <el-button type="text" icon="el-icon-edit" @click="handleUpdate(row)"
            v-hasPermi="['trade:serverCategory:edit']">修改
          </el-button>
          <el-button type="text" icon="el-icon-delete" @click="handleDelete(row)"
            v-hasPermi="['trade:serverCategory:remove']">删除
          </el-button>
          <!-- <el-button type="text" icon="el-icon-plus"
            @click="handleAdd(row)" v-hasPermi="['trade:serverCategory:add']" >新增
          </el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <!-- <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    /> -->

    <!-- 添加或修改计费规则对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="80%" append-to-body>
      <el-form ref="formRef" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="分类名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入分类名称"/>
        </el-form-item>
        <el-form-item label="分类图" prop="picUrl">
          <imageUpload v-model="form.picUrl" :limit="1"></imageUpload>
        </el-form-item>
        <el-form-item label="分类场景图" prop="topPicUrl">
          <imageUpload v-model="form.topPicUrl" :limit="1"></imageUpload>
        </el-form-item>
        <el-form-item label="分类排序" prop="sort">
          <el-input-number v-model="form.sort" :step="1" :min="0" step-strictly></el-input-number>
        </el-form-item>
        <el-form-item label="开启状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio v-for="item in dict.type.comm_kaiguan_status"
            :key="item.value"
            :label="Number(item.value)">{{item.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" :loading="popLodding" @click="submitForm">确 定</el-button>
        <el-button @click="open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import * as api from "@/api/commodity/serviceClass";
export default {
name: "ServiceClass",
dicts: ['comm_kaiguan_status'],
components: {  },
data() {
  return {
    popLodding:false,//弹窗加载
    form:{},
    // 弹出层标题
    title: "",
    // 是否显示弹出层
    open: false,
    // 遮罩层
    loading: true,
    // 显示搜索条件
    showSearch: true,
    queryParams: {// 查询参数
      pageNum: 1,name:null,status:null,
      pageSize: 10,
    },
    // 总条数
    total: 0,
    rules: {
      name: [{required: true, message: "分类名称不能为空", trigger: "blur"}],
      sort: [{required: true, message: "分类排序不能为空", trigger: "blur"}],
      status: [{required: true, message: "开启状态不能为空", trigger: "change"}],
      picUrl: [{required: true, message: "分类图不能为空", trigger: "change"}],
      topPicUrl: [{required: true, message: "分类场景图不能为空", trigger: "change"}],
    },
    list:[],//页面数据
  };
},
created() {
  this.getList()
},
methods: {
  getList() {
    this.loading = true;
    api.treeListServerCategory(this.queryParams).then(response => {
      this.list = response.data || [];
      this.loading = false;
    });
  },
  handleAdd(e) {
    this.open = true;
    this.reset();
    this.title = "添加服务分类";
    if(e) this.form.parentId = e.id
  },
  handleUpdate(row) {
    this.reset();
    const id = row.id || this.ids;
    this.open = true;
    this.title = "修改服务分类";
    api.getServerCategory(id).then(response => {
      let data = response.data || {}
      this.form = {...data};
    });
  },
  submitForm() {
    this.$refs["formRef"].validate(valid => {
      if (valid) {
        let form = {...this.form}
        if(!form.parentId) form.parentId = 0
        this.popLodding = true
        if (this.form.id != undefined) {
          api.updateServerCategory(form).then(response => {
            this.popLodding = false
              this.$modal.msgSuccess("修改成功")
              this.open = false;
              this.getList();
          }).catch(error=>this.popLodding = false);
        } else {
          api.addServerCategory(form).then(response => {
            this.popLodding = false
              this.$modal.msgSuccess("新增成功")
              this.open = false;
              this.getList()
          }).catch(error=>this.popLodding = false);
        }
      }
    });
  },
  handleDelete(row) {//删除
    const del_ids = row.id || this.ids;
    this.$modal.confirm('是否确认删除该数据项？').then(function() {
      return api.delServerCategory(del_ids);
    }).then(() => {
      this.getList();
      this.$modal.msgSuccess("删除成功");
    }).catch(() => {});
  },
  // 表单重置
  reset() {
    this.form = {status:1,};
    this.resetForm("formRef");
  },
  /** 搜索按钮操作 */
  handleQuery() {
    this.queryParams.pageNum = 1;
    this.getList();
  },
  /** 重置按钮操作 */
  resetQuery() {
    this.resetForm("queryForm");
    this.handleQuery();
  },
}
};
</script>
